package com.example.ssss.ui.loginAndRegister;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ssss.MainActivity;
import com.example.ssss.data.dao.UserDao;
import com.example.ssss.R;
import com.example.ssss.data.entity.ShareData;

/**
 * function：连接页面加载首页
 */
public class LoginAndRegisterActivity extends AppCompatActivity {
    private static final String TAG = "mysql-party-MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.setTitle("欢迎使用智安随行，请登录");
        setContentView(R.layout.activity_login_and_register);
    }

    public void reg(View view){
        Intent intent;
        intent = new Intent(LoginAndRegisterActivity.this, Register.class);
        startActivity(intent);
    }


    /**
     * function: 登录
     * */
    public void login(View view){

        EditText EditTextAccount = findViewById(R.id.uesrAccount);
        EditText EditTextPassword = findViewById(R.id.userPassword);

        new Thread(){
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                //int msg = 0;
                Message msg = Message.obtain();
                try {
                    String userID = EditTextAccount.getText().toString();
                    msg.obj = userID;
                    msg.what = userDao.login(userID,EditTextPassword.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                hand1.sendMessage(msg);
            }
        }.start();

    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 1) {
//                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
                startMain(msg.obj.toString());
            } else if (msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
            } else if (msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
            }
        }
    };

    public void startMain(String id){
        //跳转到全屏检测页面
        Intent intent = null;
        intent = new Intent(this, MainActivity.class);
        intent.putExtra("userId",id);
        this.startActivity(intent);
    }
}